﻿@page "/account/register"

@using  BlazorBoilerplate.Shared.Dto
@using BlazorBoilerplate.Shared.Dto.Account
@layout LoginLayout
@inject NavigationManager navigationManager
@inject AuthenticationStateProvider authStateProvider
@inject IMatToaster matToaster

@using static Microsoft.AspNetCore.Http.StatusCodes

<MatCard>
    <div class="logo">
        <img src="_content/BlazorBoilerplate.CommonUI/images/blazorboilerplate.svg" style="width:100px;" /><br />Blazor Boilerplate
        <br />
        <h4>Create Account</h4>
    </div>

    <EditForm Model="@registerParameters" OnValidSubmit="@RegisterUser">
        <DataAnnotationsValidator />
        <ValidationSummary />
        <fieldset>
            <div class="form-group">
                <MatTextField @bind-Value="@registerParameters.UserName" Label="User Name" Icon="person" IconTrailing="true" FullWidth="true" Required="true"></MatTextField>
            </div>
            <div class="form-group">
                <MatTextField @bind-Value="@registerParameters.Email" Label="Email" Icon="mail_outline" IconTrailing="true" FullWidth="true" Required="true"></MatTextField>
            </div>
            <div class="form-group">
                <MatTextField @bind-Value="@registerParameters.Password" Label="Password" Icon="lock_outline" IconTrailing="true" FullWidth="true" Required="true" Type="password"></MatTextField>
            </div>
            <div class="form-group">
                <MatTextField @bind-Value="@registerParameters.PasswordConfirm" Label="Password Confirmation" Icon="lock_outline" IconTrailing="true" FullWidth="true" Required="true" Type="password"></MatTextField>
            </div>
            <div class="form-group d-flex justify-content-end">
                <MatButton Type="submit" Raised="true">Create account</MatButton>
            </div>
            <hr />
            <NavLink href="account/login">
                <h6 class="font-weight-normal text-center">Already have an account? Click here to login</h6>
            </NavLink>
        </fieldset>
    </EditForm>
</MatCard>

@code {
    RegisterDto registerParameters { get; set; } = new RegisterDto();

    async Task RegisterUser()
    {
        try
        {
            var response = await ((IdentityAuthenticationStateProvider)authStateProvider).Register(registerParameters);
            if (response.StatusCode == Status200OK)
            {
                matToaster.Add("New User Email Verification was sent to: " + registerParameters.Email, MatToastType.Success, "User Creation Sucessful");
                navigationManager.NavigateTo("");
            }
            else
            {
                matToaster.Add(response.Message, MatToastType.Danger, "User Creation Failed");
            }
        }
        catch (Exception ex)
        {
            matToaster.Add(ex.Message, MatToastType.Danger, "User Creation Failed");
        }
    }
}
